<?php
/**
 * Create a new contact
 * 
 * @author Xavier VILAIN
 * @package Hive\Contact
 * @license #license-hive#
 * 
 */
use Fox\Html\Input;
use Fox\Dao\SqlFragment;
use Hive\Ressource\Source;
use Hive\Ressource;
use Fox\Generator;
use Fox\Html\Screen;
use Hive\School;
$screen = new Screen('sContact','create');
$screen->pageTitle(Msg(Hive::BUNDLE,Hive::CONTACT_CREATION_TITLE));
$block = Xview::instance()->component('ressource', 'header');
$block->id('AddRessource');
$screen->add(new Input('confirmation',false,1))->hidden();
$screen->add($block);

if($screen->submitted())
{
	$inputs = $screen->get();
	$warning = false;
	if(!$screen->errors()) {
		// Check if it's an email
		if(!empty($inputs['email']) && \Fox\Ddic\DataElement\Formater\Email::check($inputs['email'])) {
			$emails[] = $inputs['email'];
		}
		// Check if it's an email
		if(!empty($inputs['email1']) && \Fox\Ddic\DataElement\Formater\Email::check($inputs['email1'])) {
			$emails[] = $inputs['email1'];
		}
		// Lookup for existing email contact
		if(isset($emails)) {
			$result = Ressource::findEmailDuplicates($emails);
			if(count($result) > 0 && $inputs['confirmation'] == 1) {
				$warning = true;
				$contactMsg = null;
				foreach($result as $item ) {
					if(!empty($contactMsg)) {
						$contactMsg .= ',';
					}
					$contactMsg .= '<a href="?mod=ressource&act=main&rid='.$item['ressource_id'].'">'.$item['first_name'].' '.$item['last_name'].'</a>';
				}
				Fox\Message::Warning(Msg(Hive::BUNDLE,Hive::A_CONTACT_X1_IS_ALREAY_USING_THIS_EMAIL,$contactMsg));
				$screen->item('confirmation')->default = 2;
			}
		}
		
	}
	if(!$screen->errors() && !$warning)
	{
		$inputs = Fox\Utilities::arrayTrim($inputs);
		$grouping = $block->item('grouping');
		if(!empty($grouping)) {
			$inputs['grouping'] = !empty($inputs['grouping']) ? $inputs['grouping'] :
									 $block->item('grouping')->firstAvailableValue();
		}
		try {
			// Check if the school already exist
			if(!empty($inputs['school']))
			{
				$school = School::lookup($inputs['school']);
				if(!$school)
				{
					$school = new School();
					$school->setFields(array('sch_name' => $inputs['school']));
					$school->create();
				}
				$inputs['schoolid'] = $school->getId();
			}
			$ressource = new Hive\Ressource();
			// Default Creation type
			$inputs['creation_type'] = Hive\Ressource::CREATION_TYPE_STANDARD;
			$ressource->setFields($inputs);
			// Social Links
			$finder = new Hive\Ressource\Source\Type();
			$sTypes = $finder->find(new SqlFragment('active = ? ',array(1)));
			foreach($finder->find() as $type){
				if(!empty($inputs['social_'.$type->type])) {
					$source = new Source();
					$source->setFields(array('type' => $type->type,'reference' => $inputs['social_'.$type->type]));
					$ressource->addToSources($source);
				}
			}
			$ressource->create();
			$id = $ressource->getId();
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$ressource->name()));
			$screen->redirect(Generator::url('ressource','main',null,array('page' => 'personnal','rid' => $id)));
		}
		catch(Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}
echo $screen->Display();